@using Bit.Admin.Enums;
@using Bit.Core
@using Bit.Core.Enums
@using Bit.Core.AdminConsole.Enums.Provider
@using Bit.Core.Billing.Enums
@using Bit.SharedWeb.Utilities
@inject Bit.Admin.Services.IAccessControlService AccessControlService;
@inject Bit.Core.Services.IFeatureService FeatureService

@model OrganizationEditModel

@{
    var canViewGeneralDetails = AccessControlService.UserHasPermission(Permission.Org_GeneralDetails_View);
    var canViewBilling = AccessControlService.UserHasPermission(Permission.Org_Billing_View);
    var canViewPlan = AccessControlService.UserHasPermission(Permission.Org_Plan_View);
    var canViewLicensing = AccessControlService.UserHasPermission(Permission.Org_Licensing_View);
    var canEditName = AccessControlService.UserHasPermission(Permission.Org_Name_Edit);
    var canCheckEnabled = AccessControlService.UserHasPermission(Permission.Org_CheckEnabledBox);
    var canEditPlan = AccessControlService.UserHasPermission(Permission.Org_Plan_Edit);
    var canEditLicensing = AccessControlService.UserHasPermission(Permission.Org_Licensing_Edit);
    var canEditBilling = AccessControlService.UserHasPermission(Permission.Org_Billing_Edit);
    var canLaunchGateway = AccessControlService.UserHasPermission(Permission.Org_Billing_LaunchGateway);
}

<form method="post" id="edit-form" asp-route-providerId="@Model.Provider?.Id">
    <input asp-for="SalesAssistedTrialStarted" type="hidden">
    @if (canViewGeneralDetails)
    {
        <h2>General</h2>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="Name"></label>
                    <input type="text" class="form-control" asp-for="Name" value="@Model.Name" required disabled="@(canEditName ? null : "disabled")">
                </div>
            </div>
        </div>

        @if (Model.Provider?.Type == ProviderType.Reseller)
        {
            <div class="row">
                <div class="col-sm">
                    <div class="mb-3">
                        <label class="form-label">Client Owner Email</label>
                        @if (!string.IsNullOrWhiteSpace(Model.Owners))
                        {
                            <input type="text" class="form-control" asp-for="Owners" readonly>
                        }
                        else
                        {
                            <input type="text" class="form-control" asp-for="Owners" required>
                        }
                        <div class="form-text mt-0">This user should be independent of the Provider. If the Provider is disassociated with the organization, this user will maintain ownership of the organization.</div>
                    </div>
                </div>
            </div>
        }
        @if (Model.Organization != null)
        {
            <div class="form-check mb-3">
                <input type="checkbox" class="form-check-input" asp-for="Enabled" disabled='@(canCheckEnabled ? null : "disabled")'>
                <label class="form-check-label" asp-for="Enabled"></label>
            </div>
        }
    }

    @if (canViewPlan)
    {
        <h2>Plan</h2>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="PlanType"></label>
                    @{
                        var planTypes = Enum.GetValues<PlanType>()
                            .Where(p =>
                                (Model.Provider == null ||
                                p is >= PlanType.TeamsMonthly2019 and <= PlanType.EnterpriseAnnually2019 or
                                    >= PlanType.TeamsMonthly2020 and <= PlanType.EnterpriseAnnually) &&
                                (Model.PlanType == PlanType.TeamsStarter || p is not PlanType.TeamsStarter)
                            )
                            .Select(e => new SelectListItem
                            {
                                Value = ((int)e).ToString(),
                                Text = e.GetDisplayAttribute()?.GetName() ?? e.ToString()
                            })
                            .ToList();
                    }
                    <select class="form-select" asp-for="PlanType" asp-items="planTypes" disabled='@(canEditPlan ? null : "disabled")'></select>
                </div>
            </div>
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="Plan"></label>
                    <input type="text" class="form-control" asp-for="Plan" required readonly='@(!canEditPlan)'>
                </div>
            </div>
        </div>
        <h2>Features</h2>
        <div class="row mb-4">
            <div class="col-4">
                <h3>General</h3>
                <div class="form-check mb-2">
                    <input type="checkbox" class="form-check-input" asp-for="SelfHost" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="SelfHost"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="Use2fa" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="Use2fa"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseApi" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseApi"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseGroups" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseGroups"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UsePolicies" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UsePolicies"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseSso" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseSso"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseOrganizationDomains" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseOrganizationDomains"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseKeyConnector" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseKeyConnector"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseScim" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseScim"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseDirectory" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseDirectory"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseEvents" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseEvents"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseResetPassword" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseResetPassword"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseCustomPermissions" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseCustomPermissions"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseAdminSponsoredFamilies" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseAdminSponsoredFamilies"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UsePhishingBlocker" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UsePhishingBlocker"></label>
                </div>
                @if(FeatureService.IsEnabled(FeatureFlagKeys.AutomaticConfirmUsers))
                {
                    <div class="form-check">
                        <input type="checkbox" class="form-check-input" asp-for="UseAutomaticUserConfirmation" disabled='@(canEditPlan ? null : "disabled")'>
                        <label class="form-check-label" asp-for="UseAutomaticUserConfirmation"></label>
                    </div>
                }
            </div>
            <div class="col-3">
                <h3>Password Manager</h3>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseTotp" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseTotp"></label>
                </div>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UsersGetPremium" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UsersGetPremium"></label>
                </div>
            </div>
            <div class="col-3">
                <h3>Secrets Manager</h3>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseSecretsManager" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseSecretsManager"></label>
                </div>
            </div>
            <div class="col-2">
                <h3>Access Intelligence</h3>
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" asp-for="UseRiskInsights" disabled='@(canEditPlan ? null : "disabled")'>
                    <label class="form-check-label" asp-for="UseRiskInsights"></label>
                </div>
            </div>
        </div>
    }

    @if (canViewPlan)
    {
        <h2>Password Manager Configuration</h2>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="Seats"></label>
                    <input type="number" class="form-control" asp-for="Seats" min="1" readonly='@(!canEditPlan)'>
                </div>
            </div>
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="MaxCollections"></label>
                    <input type="number" class="form-control" asp-for="MaxCollections" min="1" readonly='@(!canEditPlan)'>
                </div>
            </div>
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="MaxStorageGb"></label>
                    <input type="number" class="form-control" asp-for="MaxStorageGb" min="1" readonly='@(!canEditPlan)'>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-4">
                <div class="mb-3">
                    <label class="form-label" asp-for="MaxAutoscaleSeats"></label>
                    <input type="number" class="form-control" asp-for="MaxAutoscaleSeats" min="1" readonly='@(!canEditPlan)'>
                </div>
            </div>
        </div>
    }

    @if (canViewPlan)
    {
        <div id="organization-secrets-configuration" @(Model.UseSecretsManager ? null : "lass='d-none'")>
            <h2>Secrets Manager Configuration</h2>
            <div class="row">
                <div class="col-sm">
                    <div class="mb-3">
                        <label class="form-label" asp-for="SmSeats"></label>
                        <input type="number" class="form-control" asp-for="SmSeats" min="1" readonly='@(!canEditPlan)'>
                    </div>
                </div>
                <div class="col-sm">
                    <div class="mb-3">
                        <label class="form-label" asp-for="MaxAutoscaleSmSeats"></label>
                        <input type="number" class="form-control" asp-for="MaxAutoscaleSmSeats" min="1" readonly='@(!canEditPlan)'>
                    </div>
                </div>
                <div class="col-sm">
                    <div class="mb-3">
                        <label class="form-label" asp-for="SmServiceAccounts"></label>
                        <input type="number" class="form-control" asp-for="SmServiceAccounts" min="1" readonly='@(!canEditPlan)'>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-4">
                    <div class="mb-3">
                        <label class="form-label" asp-for="MaxAutoscaleSmServiceAccounts"></label>
                        <input type="number" class="form-control" asp-for="MaxAutoscaleSmServiceAccounts" min="1" readonly='@(!canEditPlan)'>
                    </div>
                </div>
            </div>
        </div>
    }

    @if(canViewLicensing)
    {
        <h2>Licensing</h2>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="LicenseKey"></label>
                    <input type="text" class="form-control" asp-for="LicenseKey" readonly='@(!canEditLicensing)'>
                </div>
            </div>
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="ExpirationDate"></label>
                    <input type="datetime-local" class="form-control" asp-for="ExpirationDate" readonly='@(!canEditLicensing)' step="1">
                </div>
            </div>
        </div>
    }

    @if (canViewBilling)
    {
        <h2>Billing</h2>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="BillingEmail"></label>
                    <input type="email" class="form-control" asp-for="BillingEmail" readonly="readonly">
                </div>
            </div>
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="Gateway"></label>
                    <select class="form-select" asp-for="Gateway" disabled="@(!canEditBilling)"
                            asp-items="Html.GetEnumSelectList<Bit.Core.Enums.GatewayType>()">
                        <option value="">--</option>
                    </select>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="GatewayCustomerId"></label>
                    <div class="input-group">
                        <input type="text" class="form-control" asp-for="GatewayCustomerId" readonly='@(!canEditBilling)'>
                        @if(canLaunchGateway)
                        {
                            <button class="btn btn-secondary" type="button" id="gateway-customer-link">
                                <i class="fa fa-external-link"></i>
                            </button>
                        }
                    </div>
                </div>
            </div>
            <div class="col-sm">
                <div class="mb-3">
                    <label class="form-label" asp-for="GatewaySubscriptionId"></label>
                    <div class="input-group">
                        <input type="text" class="form-control" asp-for="GatewaySubscriptionId" readonly='@(!canEditBilling)'>
                        @if (canLaunchGateway)
                        {
                            <button class="btn btn-secondary" type="button" id="gateway-subscription-link">
                                <i class="fa fa-external-link"></i>
                            </button>
                        }
                    </div>
                </div>
            </div>
        </div>
    }
</form>
